﻿using System.Linq;
using ParserGenerator.Exceptions;
using ParserGenerator.Extensions;

namespace ParserGenerator.SyntacticAnalysis
{
    partial class StringLiteral
    {
        public bool IsEmpty => Value.Length == 0;

        public bool IsSingle
        {
            get
            {
                if (IsEmpty)
                    return false;
                bool first = true;
                foreach (int c in Value.EnumCodepoints())
                {
                    if (first)
                        first = false;
                    else
                        return false;
                }
                return true;
            }
        }

        public bool IsMultiple
        {
            get
            {
                if (IsEmpty)
                    return false;
                bool first = true;
                foreach (int c in Value.EnumCodepoints())
                {
                    if (first)
                        first = false;
                    else
                        return true;
                }
                return false;
            }
        }

        public int AsSingle
        {
            get
            {
                if (!IsSingle)
                {
                    ValueErrorException e = new ValueErrorException(SR.StringLiteralNotSingle);
                    e.AddLocation(Context);
                    throw e;
                }
                return Value.EnumCodepoints().First();
            }
        }
    }
}
